﻿#region License
/**	
The MIT License (MIT)

Copyright (c) 2013 Philipp Schillinger, Michael Alekseew, Andreas Braun, Steeven Zeiß

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. 
 **/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IndoorLocalization {

	/// <summary>
	/// Receives rays from sensors and combines them to find valid entities.
	/// This class implements a merger that does not exist but represents a shadow
	/// instance of a local merger of another application.
	/// This merger does not run a merge thread but sends all rays to the corresponding
	/// local merger and will never have entities.
	/// </summary>
	[Serializable()]
	public class RemoteMerger : SensorMerger {

		private byte networkID;
		/// <summary>
		/// The network ID of the application running the corresponding local merger.
		/// </summary>
		public byte NetworkID { get { return networkID; } }

        /// <summary>
        /// Creates a new remote merger for a bunch of sensors.
        /// Each sensor that should be merged by this merger has to specify this merger
        /// as its one.
        /// A remote merger can only be created if the corresponding local merger of an other
		/// application is connected.
		/// </summary>
		/// <param name="name">Unique name for this merger to be displayed, equal to the name of the local merger.</param>
		/// <param name="localID">Network id of the application that has the corresponding local merger.</param>
        public RemoteMerger(string name, byte localID) {
			this.name = name;
			this.networkID = localID;

			StartMerger();
        }

		/// <summary>
		/// Initializes the important attributes and starts the merge thread.
		/// </summary>
		protected override void StartMerger() {
			// initialize attributes
			rays = new List<SensorRay>();
			entities = new List<Entity>();

			// nothing else to do
		}

		/// <summary>
		/// Indicates whether the merge thread of this merger is running.
		/// </summary>
		public override bool IsRunning() {
			// in fact, nothing is running here, but it can be considered as running
			// because everything is reported to another merger
			return true;
		}

		/// <summary>
		/// Indicates whether this merger is local or remote.
		/// </summary>
		public override bool IsLocal() {
			return false;
		}

		/// <summary>
		/// Updates the valid rays of the given sensor
		/// by deleting all old rays and adding the new ones.
		/// In fact, those rays will be sent via the network to the corresponding local merger.
		/// </summary>
		/// <param name="sensor">Sensor of which the rays should be updated.</param>
		/// <param name="newRays">List of currently valid rays.</param>
		public override void UpdateRaysTS(Guid sensor, List<SensorRay> newRays) {
			NetworkConnector.SendRaysToRemoteMerger(this, sensor, newRays);
		}

	}

}
